package com.asia.app.csv;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;

public class Test {

    public static void main(String[] args) {
        String path ="d:\\";
        ArrayList<String> fileLists = new ArrayList<String>();
        StringBuffer sb = new StringBuffer();
        sb.append("ϦСǧ����������������ʹʼ�����˼��������������ʩ��ٻɺʦʱ�������ϯЦ����巳˴����������ϳ��b�ʾ��ݾ���Ȼ����˹������ʫ��������������ʵ�����������ϻ��?˫����ܰ��������һ��Ѿ���������������ٰ������س������ί�������Ȱ���������Τ��ԫ�h����̶���������Ψ����ξ�ӭ����Ϊ���ӽ��ڼ�Z԰Բ������覾���tά��Ҷ���γ����Ӱŷ���ݺ�����Ӧ��������Զ�����Ծܲ��");
        ArrayList<String> result = new ArrayList<String>();
        for  (int index = 0;index <sb.length();index++)
        {
            for (int count = 0;count < sb.length();count++)
            {
                result.add(sb.substring(index,index+1)+sb.substring(count,count+1));
            }
        }
       // System.out.print(result);
        File f = new File("d:\\name.txt");

        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(f, true));
        for (int index = 0;index < result.size();index++)
        {
            bw.write(result.get(index) + "  ");
        }
            bw.close();
        } catch (IOException ioe) {
        ioe.printStackTrace();
    }

       // fileLists.add("d:\\1.log");
      //  fileLists.add("d:\\1.log");
       // Integer.parseInt("  5C  ".trim(),16).toString();//����
        //for (int index = 0;index <= 200;index++)
       // {
       //     fileLists.add("d:\\CJFGPRS_P5020140211112148321290");
       // }
       // long time1s = System.currentTimeMillis();
       // Test.mergeFiles("d:\\1.SVL",fileLists);
       // long time1e = System.currentTimeMillis();
      //  long time2s = System.currentTimeMillis();

     //   Test.mergeFilesByLine("d:\\2.SVL",fileLists);
      //  long time2e = System.currentTimeMillis();
        //long time3s = System.currentTimeMillis();
       // Test.mergeFileForReader("d:\\3.SVL",fileLists);
       // long time3e = System.currentTimeMillis();
       // System.out.println("ԭʼ�ٶ�:   " +  String.valueOf(time1e - time1s));
      //  System.out.println("�޸�nio�ٶ�:   " +  String.valueOf(time2e - time2s));
       // System.out.println("ֱ�Ӷ�ȡһ���ٶ�:   " +  String.valueOf(time3e - time3s));
    }

    public static boolean mergeFiles(String outFile, ArrayList<String> files)
    {
        int BUFSIZE = 1341;
        FileChannel outChannel = null;
        try {
            outChannel = new FileOutputStream(outFile, true).getChannel();
            for (String f : files) {
                FileChannel fc = new FileInputStream(f).getChannel();
                ByteBuffer bb = ByteBuffer.allocate(BUFSIZE);
                while (fc.read(bb) != -1) {
                    byte a = 1;
                    bb.put(111,a);
                    bb.flip();
                    outChannel.write(bb);
                    bb.clear();
                }
                fc.close();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                if (outChannel != null)
                    outChannel.close();
            }
            catch (IOException ignore) {
                ignore.printStackTrace();
            }
        }
        return true;
    }
    //̫����
    public static boolean mergeFileForReader(String mFileName, ArrayList<String> files)
    {
        try
        {
            for (String file : files) {
            InputStreamReader is = new InputStreamReader(new FileInputStream(
                    file), "gbk");
            BufferedReader br = new BufferedReader(is);
            File f = new File(mFileName);
            String s;
            while ((s = br.readLine()) != null)
            {

                BufferedWriter bw = new BufferedWriter(new FileWriter(f, true));
                bw.write(s);
                bw.newLine();
                bw.close();
            }

            br.close();
            }
        }
        catch (Exception e)
        {

            e.printStackTrace();
         }
        return true;
    }
    public static boolean mergeFilesByLine(String outFile, ArrayList<String> files)
    {
        int BUFSIZE = 1342;
        FileChannel outChannel = null;
        String enterStr = "\n";
        try {
            outChannel = new FileOutputStream(outFile, true).getChannel();
            for (String f : files) {
                FileChannel fc = new FileInputStream(f).getChannel();
                ByteBuffer bb = ByteBuffer.allocate(BUFSIZE);
                ByteBuffer wBuffer = ByteBuffer.allocateDirect(BUFSIZE);
                byte[] bs = new byte[BUFSIZE];


                int size = 0;
                StringBuffer strBuf = new StringBuffer("");
                while (fc.read(bb) != -1) {
                    int rSize = bb.position();
                    bb.rewind();
                    bb.get(bs);
                   // bb.clear();
                    String tempString = new String(bs, 0, rSize);
                    int fromIndex = 0;
                    int endIndex = 0;

                    while((endIndex = tempString.indexOf(enterStr, fromIndex)) != -1)
                    {
                        String line = tempString.substring(fromIndex, endIndex);
                        Integer.parseInt(line.substring(42,49).trim(),16);
                        if  (bs[42]> 60)
                        {
                            int a = bs[42] - 55 * 16 ^4;
                        }

                        if (bs[50] == 76 && bs[51] == 84 && bs[52] == 69 && bs[53] == 32 )
                        {
                            //Integer.parseInt(line.substring(54,57).trim(),16);//����
                            String.valueOf(Integer.parseInt(line.substring(54,57).trim(),16));
                        }
                        else
                        {
                            Integer.parseInt(line.substring(54, 59).trim(), 16);
                        }

                        line = new String(strBuf.toString() + line);

                        line.substring(50, 54);
                        writeFileByLine(outChannel, wBuffer, line + enterStr);
                        strBuf.delete(0, strBuf.length());
                        fromIndex = endIndex + 1;
                    }
                    if(rSize > fromIndex){
                        strBuf.append(tempString.substring(fromIndex, tempString.length()));
                    }
                    else
                    {
                        strBuf.delete(0, strBuf.length());
                    }

                    bb.clear();
                }
                if (strBuf.length() != 0)
                {
                    writeFileByLine(outChannel, wBuffer, strBuf.toString());
                }
                fc.close();
            }
        } catch (IOException ioe) {
            System.out.println("�ϲ��ļ��쳣�쳣!" + ioe.getMessage());
            ioe.printStackTrace();
           return false;
        } finally {
            try {
                if (outChannel != null)
                    outChannel.close();
            }
            catch (IOException ignore) {
                ignore.printStackTrace();
            }
        }
        return true;
    }
    public static void writeFileByLine(FileChannel fcout, ByteBuffer wBuffer, String line){
        try {
            fcout.write(wBuffer.wrap(line.getBytes(Charset.forName("ISO-8859-1"))), fcout.size());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static  void getFileList(String path){
        File rootDir = new File(path);
        if(!rootDir.isDirectory()){
            System.out.println("�ļ���"+rootDir.getAbsolutePath());
        }else{
            String[] fileList =  rootDir.list();
            for (int i = 0; i < fileList.length; i++) {
                path = rootDir.getAbsolutePath()+"\\"+fileList[i];

                System.out.println(path);
                getFileList(path);
            }
        }
        return;
    }
}
